package studyguide.ch09.customitem;

import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Graphics;

public class CustomItemC extends CustomItem {

	private static final int MIN_COMPONENT_WIDTH = 70;
	private static final int MIN_COMPONENT_HEIGHT = 20;
	
	private final int borderColor; 
	private final int hlBorderColor; 
	
	private int currentBorderColor;
	
	protected CustomItemC(String label, int borderColor, int hlBorderColor) {
		super(label);
		this.borderColor = borderColor;
		this.hlBorderColor = hlBorderColor;
		

		// Check if device supports traversing
		int iModes = getInteractionModes();
		if ((TRAVERSE_HORIZONTAL | iModes) == 0) {
			throw new RuntimeException("Traversing not supported");
		}
		if ((TRAVERSE_VERTICAL | iModes) == 0) {
			throw new RuntimeException("Traversing not supported");
		}
		
		currentBorderColor = borderColor;
	}

	protected int getMinContentHeight() {
		return MIN_COMPONENT_HEIGHT;
	}

	protected int getMinContentWidth() {
		return MIN_COMPONENT_WIDTH;
	}

	protected int getPrefContentWidth(int height) {
		return MIN_COMPONENT_WIDTH;
	}

	protected int getPrefContentHeight(int width) {
		return MIN_COMPONENT_HEIGHT;
	}
	
	protected void paint(Graphics g, int w, int h) {
		// Clear screen:
		g.setColor(0x00ffffff);// White
		g.fillRect(0, 0, w, h);
		
		// Draw a border
		g.setColor(currentBorderColor);
		g.drawRect(0, 0, w-1, h-1);
	}

	protected boolean traverse(
			int dir, 
			int viewportWidth, int viewportHeight,
			int[] visRect_inout) {
		// Traversal is used only to change the border color. No internal traversing is
		// supported
		currentBorderColor = hlBorderColor;
		repaint();
		return false;
	}

	protected void traverseOut() {
		currentBorderColor = borderColor;
		repaint();
	}

}
